import HttpUtil from "../lib/HttpUtil";
import PlayerData from "./data/PlayerData";

export default class RankManager{
    private static _ins:RankManager;
    private mDataUrl:string="https://50e16a1e-3090-4b8e-b0dd-44adc6379583.bspapp.com/http/get";
    private mSendUrl:string="https://50e16a1e-3090-4b8e-b0dd-44adc6379583.bspapp.com/http/update";
    private mAddUrl:string="https://50e16a1e-3090-4b8e-b0dd-44adc6379583.bspapp.com/http/add";
    private mGetOpenIdUrl:string="https://50e16a1e-3090-4b8e-b0dd-44adc6379583.bspapp.com/http/getOpenId";
    private mDBName="rank_paopao";
    private mData;
    constructor(){

    }

    public getRankData(_func:Function=null){
        if(this.mData){
            if(_func)_func(this.mData);
        }else{
            let tdata={dbname:this.mDBName,id:PlayerData.getIns().id,isList:true,count:50,sortKey:"score",sortType:"desc"};
            HttpUtil.get(this.mDataUrl,(_data)=>{
                this.mData=_data;
                if(_func)_func(this.mData);
            },tdata);
        }
    }

    public updateScore(_score:number){
        let tdata:any={dbname:this.mDBName,id:PlayerData.getIns().id,score:_score};
        if(PlayerData.getIns().name)tdata.name=PlayerData.getIns().name;
        HttpUtil.get(this.mSendUrl,(_data)=>{
           console.log(_data);
        },tdata);
    }

    public updateName(){
        let tdata:any={dbname:this.mDBName,id:PlayerData.getIns().id,name:PlayerData.getIns().name};
        HttpUtil.get(this.mSendUrl,(_data)=>{
          if(this.mData){
              for(let i=0;i<this.mData.length;i++){
                    if(this.mData[i].id==PlayerData.getIns().id){
                        this.mData[i].name=PlayerData.getIns().name;
                        break;
                    }
              }
          }
        },tdata);
    }

    public updateHead(){
        let tdata:any={dbname:this.mDBName,id:PlayerData.getIns().id,head:PlayerData.getIns().head};
        HttpUtil.get(this.mSendUrl,(_data)=>{
          if(this.mData){
              for(let i=0;i<this.mData.length;i++){
                    if(this.mData[i].id==PlayerData.getIns().id){
                        this.mData[i].head=PlayerData.getIns().head;
                        break;
                    }
              }
          }
        },tdata);
    }

    public addDataToDB(){
        let tdata={dbname:this.mDBName,id:PlayerData.getIns().id,name:PlayerData.getIns().name,score:0,head:PlayerData.getIns().head,openId:PlayerData.getIns().openId,_id:PlayerData.getIns().id};
        HttpUtil.post(this.mAddUrl,tdata,(_data)=>{
            console.log(_data);
         });
    }

    public getOpenId(_appid,_code,_anonymous_code,_func){
        let tdata={appid:_appid,code:_code,anonymous_code:_anonymous_code};
        HttpUtil.get(this.mGetOpenIdUrl,(_data)=>{
            _func(_data);
        },JSON.stringify(tdata));
    }

    public getListData(){
        return this.mData?this.mData:[];
    }

    public static getIns():RankManager{
        if(!this._ins)this._ins=new RankManager();
        return this._ins;
    }
}